<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 增加一个作用域插槽 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartmentFn(data)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDpart(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.id !== '604e115c7bfcfa45d019d3e9'" @click.native="delFn(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 弹框 -->
    <addDpartments
      ref="addDpart"
      :show-dialog="showDialog"
      :cur-node="curNode"
      :code-list="codeList"
      @closeDialog="closeDialogFn"
      @update-depart="hGetDepartment"
    />
    <!-- 当点击叉号的时候 并没有修改父组件中的dialogvisible
  在组件内部触发了一个自定义事件 -->
  </div>
</template>
''
<script>
import { getDepartment, delDepartments } from '@/api/dapartment'
import { transTree } from '@/utils/transTreeData'
import addDpartments from './components/addDepartments'
export default {
  components: {
    addDpartments
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      curNode: null,
      codeList: []
    }
  },
  mounted() {
    // 打开窗口 获取调用获取组织架构方法
    this.hGetDepartment()
  },
  methods: {
    // 点叉号关闭弹窗
    closeDialogFn() {
      this.showDialog = false
    },
    // 获取组织架构列表
    async hGetDepartment() {
      // 处理函数
      // 页面初始化的时候有可能会发送n个请求 n个请求之间还可能相互依赖
      // 为了方便维护 生命周期钩子函数只做初始化的事情 尽量简单
      // 不要在生命周期钩子函数前面随便加async
      const res = await getDepartment()
      console.log(res)
      // 用来做校验的未经处理的原数组
      this.codeList = res.data.depts.map(item => item.code)
      this.list = transTree(res.data.depts)
    },
    // 删除部门
    delFn(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        // 删除操作成功
        await delDepartments(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.hGetDepartment()
      }).catch(error => {
        console.log(error)
      })
    },
    // 新增部门
    addDepartmentFn(data) {
      // 打开弹窗
      this.showDialog = true
      // 记录当前点击项的数据
      this.curNode = data
    },
    editDpart(id) {
      // 1.打开弹框
      this.showDialog = true
      // 3.通知子组件进行数据获取回显
      this.$refs.addDpart.hGetDepartDetail(id)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
